<?php

require_once 'BaseHelper.class.php';

class CatalogHelper extends BaseHelper {
	
	public static $MODULE_NAME = "Catalog"; //数据库操作的模块名

	//------------------------------------基本函数开始----------------------------------------
	public static function add($data,$displaySQL = false) {
		return parent::add ( self::$MODULE_NAME, $data,$displaySQL );
	}
	public static function update($data, $whereStr="",$displaySQL = false) {
		return parent::update ( self::$MODULE_NAME, $data, $whereStr,$displaySQL );
	}
	public static function updateSQL($sql,$displaySQL = false) {
		return parent::updateSQL( self::$MODULE_NAME, $sql,$displaySQL);
	}
	public static function delete($whereStr="",$displaySQL = false) {
		return parent::delete ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function getObject($whereStr="",$displaySQL = false) {
		return parent::getObject ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function queryObject($sql,$displaySQL = false) {
		return parent::queryObject ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function queryForInt($sql,$displaySQL = false) {
		return parent::queryForInt ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function getList($whereStr = '', $orderStr = '', $limitNum = 0,$displaySQL = false) {
		return parent::getList ( self::$MODULE_NAME, $whereStr, $orderStr, $limitNum ,$displaySQL);
	}
	public static function queryList($sql = '', $displaySQL = false) {
		return parent::queryList(self::$MODULE_NAME,$sql,$displaySQL);
	}
	//------------------------------------基本函数结束----------------------------------------


	//------------------------------------自定义函数开始--------------------------------------
	
	
	//供递归算法使用。后台[商品分类列表]
	public static function getCatalogTree($rootid,$subList,$counter=1) {
		$str = '';
		foreach ($subList as $obj){
			if($obj["pid"] == $rootid){	//为子类，继续向下递归
				$str .= "<tr bgcolor='#FFFFFF' class='{$counter}' name='line_hover'>";
				$str .= "<td height='20' style='padding-left: 10px;'><img src='__IMAGES4ADMIN__/menu_minus.gif' style='margin-left:{$counter}em' onclick='rowClicked(this)' /><span>&nbsp;{$obj["catalog_name"]}</span></td>";
				$str .= "<td height='20'><div align='center'>".CommonUtil::getCataType($obj)."</div></td>";
				$str .= "<td height='20'><div align='center'><span><a href='".LinkUtil::getCataLink($obj)."' target='_blank'>访问</a></span></div></td>";
				$str .= "<td height='20'><div align='center'><span class='span_block span_block{$obj["catalog_id"]}'><span class='span_{$obj["catalog_id"]}'>{$obj["order_num"]}</span><a href='javascript:showChangeOrderNum({$obj["catalog_id"]});' target='_self'>修改</a></span>&nbsp;<span class='action_block action_block{$obj["catalog_id"]}' style='display:none;'><input type='text' class='order_{$obj["catalog_id"]} order' id='order_{$obj["catalog_id"]}' value='{$obj["order_num"]}' style='width:80px;'/>&nbsp;<input type='button' name='savebtn' value='保存' onclick='saveOrderNum({$obj["catalog_id"]})'/></span></div></td>";
				$str .= "<td height='20'><div align='center'><a href='__APP__/center/catalog/todo/?id={$obj["catalog_id"]}'>编辑&nbsp;|&nbsp;</a><a href=\"javascript:toConfirm('是否确定删除该数据？','__APP__/admin/catalog/delete/?id={$obj["catalog_id"]}');\" target='_self'>删除</a></div></td>";
				$str .= "</tr>";
				$subStr = self::getCatalogTree($obj["catalog_id"],$subList,$counter+1);
				$str .= $subStr;
			}
		}
		return $str;
	}
	
	//递归算法使用。[栏目分类下拉框]
	public static function getCatalogSelectTree($rootid,$subList,$pid,$counter=1) {
		$str = '';
		foreach ($subList as $obj){
			if($obj["pid"] == $rootid){	//为子类，继续向下递归
				$padding_str = "";
				for ($i=0;$i<$counter;$i++){
					$padding_str .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				$select_str = "";
				if($obj["catalog_id"] == $pid){
					$select_str = 'selected="selected"';
				}
				$str .= '<option value="'.$obj["catalog_id"].'" '.$select_str.'>'.$padding_str.$obj['catalog_name'].'</option>';
				$subStr = self::getCatalogSelectTree($obj["catalog_id"],$subList,$pid,$counter+1);
				$str .= $subStr;
			}
		}
		return $str;
	}
	
	//供递归算法使用。后台栏目导航地址
	public static function getAdminSubitemStr($catalogId,$subCatalogList) {
		$str = '';
		foreach ($subCatalogList as $obj){
			if($obj["pid"] == $catalogId){	//为子类，继续向下递归
				$subitemStr = CatalogHelper::getAdminSubitemStr($obj["catalog_id"],$subCatalogList);
				$note_str = "";
				if(empty($subitemStr)){
					$note_str = '<span>'.$obj["catalog_name"].'</span>';
				}else{
					$note_str = "<table width='100%' border='0' cellpadding='0' cellspacing='0' style='padding-right:12px;'>";
					$note_str .= "<tr>";
					$note_str .= "<td>";
					$note_str .= '<span>'.$obj["catalog_name"].'</span>';
					$note_str .= "</td>";
					$note_str .= "<td align='right'>";
					$note_str .= "<i class='qu'></i>";
					$note_str .= "</td>";
					$note_str .= "</tr>";
					$note_str .= "</table>";
					
				}
				$str .= '<li><a href="'.CatalogHelper::getAdminItemAddr($obj).'">'.$note_str.'</a>';
				$str .= $subitemStr;
				$str .= '</li>';
			}
		}
		if(!empty($str)){
			$str = '<div><ul>'.$str."</ul></div>";
		}
		return $str;
	}
	
	//供递归算法使用。后台栏目导航地址
	public static function getSubCataStr($catalog,$isFirst=TRUE) {
		$str = '';
		$subCatalogList = CatalogHelper::getList("pid={$catalog['catalog_id']} AND catalog_type='{$catalog['catalog_type']}'");
		foreach ($subCatalogList as $key=>$obj){
			if($isFirst == FALSE || $key != 0){
				$str .= ",";
			}
			$str .= $obj["catalog_id"];
			$list = CatalogHelper::getList("pid={$obj['catalog_id']} AND catalog_type='{$obj['catalog_type']}'");
			if(count($list) != 0){
				$str .= CatalogHelper::getSubCataStr($obj,FALSE);
			}
		}
		return $str;
	}
	/**
	 * 
	 * 获取到后台ITEM导航的地址
	 * @param unknown_type $catalog
	 * @param unknown_type $hasSubItem
	 */
	public static function getAdminItemAddr($catalog){
		$str = "javascript:;";
		if($catalog["catalog_type"] == CatalogType::$SINGEL_ARTICLE){
			$str = __APP__."/center/singel_article/todo/?cid=".$catalog["catalog_id"];
		}elseif ($catalog["catalog_type"] == CatalogType::$DYNAMIC_MODULE){
			$str = __APP__."/center/dynamicModule/?cid=".$catalog["catalog_id"];
		}elseif ($catalog["catalog_type"] == CatalogType::$GUESTBOOK){
			$str = __APP__."/center/GuestBook/?cid=".$catalog["catalog_id"];
		}elseif ($catalog["catalog_type"] == CatalogType::$FM_PAGE){
			$str = __APP__."/center/fm/?cid=".$catalog["catalog_id"];
		}elseif ($catalog["catalog_type"] == CatalogType::$RESERVE_MODULE){
			$str = __APP__."/center/reserve/?cid=".$catalog["catalog_id"];
		}elseif ($catalog["catalog_type"] == CatalogType::$REDIRECT){
			//进行无限功能扩展...
			/*if($catalog["redirect_url"] == "/reserve/"){	//在线预定后台
				$str = __APP__."/center/reserve/";
			}*/
		}
		return $str;
	}
	
	
	/**
	 * 刷新类型以及子类型【事务控制】
	 */
	public static function refreshCataWithTrans($data,$id) {
		
		$model = M();
		$model->startTrans();
		
		//1........更新该节点的PID和ROOTID
		$result1 = CatalogHelper::update($data,"catalog_id={$id}");
		//2........更新该子节点的PID和ROOTID
		$list = CatalogHelper::getSubTypeList($id);
		$result2 = true;
		foreach ($list as $obj){
			$data2["root_id"] = $data["root_id"];
			$r = CatalogHelper::update($data2,"catalog_id={$id}");
			if($r === false){
				$result2 = false;
				break;
			}
		}
		
		if($result1 !== false && $result2 !== false){
			$model->commit();
			return true;
		}else{
			$model->rollback();
			return false;
		}
		
	}
	
	/**
	 * 获取该ID下的所有子子孙孙节点，[递归]
	 */
	public static function getSubTypeList($id,$counter=0){
		$arr = array();
		$list = CatalogHelper::getList("pid={$id}");
		foreach ($list as $obj){
			$counter++;
			$arr[$counter] = $obj;
			$arr = array_merge($arr,self::getSubTypeList($obj["catalog_id"],$counter));
		}
		return $arr;
	}
	
	/**
	 * 获取当前页面位置
	 */
	public static function getLocation($catalog) {
		$locationArr = array();
		
		$cataTemp = $catalog;
		$catalog["link"] = LinkUtil::getCataLink($catalog);
		
		$locationArr[0] = $catalog;
		
		$i = 1;
		while (true){
			if($i > 10){	//防止程序中出现死循环
				break;
			}
			if($cataTemp["pid"] == 0){	//无父亲节点，则无需再进行循环找子节点
				break;
			}
			$p_cata = CatalogHelper::getObject("catalog_id=".$cataTemp["pid"]);
			$cataTemp = $p_cata;
			$p_cata["link"] = LinkUtil::getCataLink($p_cata);
			$locationArr[$i] = $p_cata;
			$i++;
		}
		
		$index_cata["catalog_name"] = "首页";
		$index_cata["link"] = __APP__."/";
		$locationArr[$i] = $index_cata;
		
		$tarr = array_reverse($locationArr);
		
		$str = '';
		$j=0;
		foreach ($tarr as $cata){
			if($j != 0){
				$str .= '&nbsp;&gt;&nbsp;';
			}
			$str .= '<a href="'.$cata["link"].'">'.$cata["catalog_name"].'</a>';
			$j++;
		}
		return $str;
	}
	
	
	/**
	 * 获取后台当前导航位置
	 */
	public static function getLocation4Admin($catalog) {
		$locationArr = array();
		
		$cataTemp = $catalog;
		$catalog["link"] = CatalogHelper::getAdminItemAddr($catalog);
		
		$locationArr[0] = $catalog;
		
		$i = 1;
		while (true){
			if($i > 10){	//防止程序中出现死循环
				break;
			}
			if($cataTemp["pid"] == 0){	//无父亲节点，则无需再进行循环找子节点
				break;
			}
			$p_cata = CatalogHelper::getObject("catalog_id=".$cataTemp["pid"]);
			$cataTemp = $p_cata;
			$p_cata["link"] = CatalogHelper::getAdminItemAddr($p_cata);
			$locationArr[$i] = $p_cata;
			$i++;
		}
		
		$index_cata["catalog_name"] = "首页";
		$index_cata["link"] = __APP__."/center/frame/index/";
		$locationArr[$i] = $index_cata;
		
		$tarr = array_reverse($locationArr);
		
		$str = '';
		$j=0;
		foreach ($tarr as $cata){
			if($j != 0){
				$str .= '&nbsp;&gt;&nbsp;';
			}
			$str .= '<a href="'.$cata["link"].'">'.$cata["catalog_name"].'</a>';
			$j++;
		}
		return $str;
	}
	
	//------------------------------------自定义函数结束--------------------------------------


}